package com.project.controller;


import com.project.bean.OrderBean;
import com.project.bean.OrderInfoBean;
import com.project.bean.SendInfoBean;
import com.project.bean.UserBean;
import com.project.dto.riderToUser.InfoDto;
import com.project.dto.userToMerchant.OrderDto;
import com.project.dto.userToMerchant.OrderInfoDto;
import com.project.service.IOrderService;
import com.project.service.ISendInfoService;
import com.project.service.remote.RiderRemoteService;
import com.project.thread.OrderThread;
import com.project.util.JwtUtil;
import com.project.util.codingUtils.SnowflakeIdWorker;
import com.project.vo.R;
import io.swagger.annotations.Api;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 订单表（用户模块） 前端控制器
 * </p>
 *
 * @author lhy
 * @since 2021-10-05
 */
@RestController
@Api(tags = "订单测试接口")
public class OrderController extends ApiBaseController {

    @Autowired
    private IOrderService orderService;
    @Autowired
    private ISendInfoService sendInfoService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private RiderRemoteService riderRemoteService;


    @GetMapping("addOrder")
    public R addOrder(HttpServletRequest request, String[] infoList,
                      String description, String shopCode) {
        List<OrderInfoBean> infoList1 = new ArrayList<>();
        for (int i = 0; i < infoList.length; i = i + 5) {
            OrderInfoBean infoBean = new OrderInfoBean(Integer.parseInt(infoList[i]), infoList[i + 1],
                    infoList[i + 2], infoList[i + 3], Integer.parseInt(infoList[i + 4]));
            infoList1.add(infoBean);
        }
        Long orderCode = new SnowflakeIdWorker(1, 3).nextId();
        UserBean userBean = (UserBean) request.getSession().getAttribute("loginUser");
        SendInfoBean sendInfoBean = sendInfoService.findById(userBean.getSendInfoId());
        OrderBean orderBean = new OrderBean(orderCode + "", description,
                sendInfoBean.getAddress(), sendInfoBean.getReceiver(), sendInfoBean.getGentle(),
                sendInfoBean.getPhone(), userBean.getUserCode(), shopCode);
        //设置订单支付过期时间
        new Thread(new OrderThread(orderService,orderCode+"",1)).start();
        return R.ok().data("order", orderService.addOrder(infoList1, orderBean));
    }

    @GetMapping("findOrderByItem")
    public R findOrderByItem(HttpServletRequest request, Integer status) {
        UserBean userBean = (UserBean) request.getSession().getAttribute("loginUser");
        List<OrderBean> orderList = orderService.findOrderByItem(status, userBean.getUserCode());
        return R.ok().data("orderList", orderList);
    }

    @GetMapping("updateOrderStatusZeroToOne")
    public R updateOrderStatus(HttpServletRequest request, String orderCode, String openid) {
        UserBean userBean = JwtUtil.verifierJwt(openid);
        UserBean userBean1 = (UserBean) request.getSession().getAttribute("loginUser");
        if (!userBean.getOpenid().equals(userBean1.getOpenid())) {
            return R.error().message("tokenError");
        }
        OrderBean orderBean = orderService.updateOrderStatus(orderCode, 1);
        if (orderBean == null) {
            return R.error().message("orderError");
        }
        // 组装关联数据  mq远程发送给商家模块
        List<OrderInfoDto> infoDtoList = new ArrayList<>();
        List<OrderInfoBean> infoBeanList = orderBean.getInfoList();
        for (int i = 0; i < infoBeanList.size(); i++) {
            OrderInfoDto dto = new OrderInfoDto();
            BeanUtils.copyProperties(infoBeanList.get(i), dto);
            infoDtoList.add(dto);
        }
        OrderDto orderDto = new OrderDto();
        BeanUtils.copyProperties(orderBean, orderDto);
        orderDto.setInfoList(infoDtoList);
        CorrelationData correlationData = new CorrelationData(orderCode);
        rabbitTemplate.convertAndSend("userDirectExchange", "User.Queue", orderDto, correlationData);
        return R.ok().data("order", orderBean);
    }

    @GetMapping("commentRider")
    public R commentOrder(String orderCode, String rating) {
        //todo 远程调用商家订单评价接口
        InfoDto infoDto = riderRemoteService.userRating(orderCode, rating);
        if (infoDto.getCode() == 1) {
            orderService.updateOrderStarStatus(orderCode);
            return R.ok().data("infoDto", infoDto);
        }
        return R.error().data("infoDto", infoDto);
    }


}
